const jwt = require('jsonwebtoken')
const whiteList = ['/api/login', '/api/register']
// 就是一个函数
const auth = async (ctx, next) => {
	if (whiteList.includes(ctx.path)) {
		await next()
	} else {
		try {
			const { token } = ctx.request.headers
			const userInfo = jwt.verify(token, '1904A')
			ctx.state.userInfo = userInfo
			await next()
		} catch (err) {
			ctx.body = {
				msg: err
			}
		}
	}
}

module.exports = {
	auth
}
